package com.mayblackcat.dy.service.impl;

import com.mayblackcat.dy.FrontEndData.AjaxResult;
import com.mayblackcat.dy.dao.UserDao;
import com.mayblackcat.dy.dao.VideoDao;
import com.mayblackcat.dy.entity.User;
import com.mayblackcat.dy.entity.Video;
import com.mayblackcat.dy.exception.NotFoundException;
import com.mayblackcat.dy.service.UpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;


@Service
public class UpLoadServiceImpl implements UpLoadService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private VideoDao videoDao;

    //文件上传
    public AjaxResult upLoad(MultipartFile file,String title,String des,long id){

        if (!file.isEmpty()) {
            try {
                //判断文件格式是否正确
                String fileName = file.getOriginalFilename().toLowerCase();
                if (!fileName.endsWith(".bmp") && !fileName.endsWith(".jpg")
                        && !fileName.endsWith(".jpeg") && !fileName.endsWith(".png")
                        && !fileName.endsWith(".gif")&& !fileName.endsWith(".mp4")) {
                    return new  AjaxResult(AjaxResult.Type.ERROR,"fail");
                }
                //存放路径
                String filePath = new File("D:\\javaproject\\dy\\src\\main\\resources\\static\\dy_app").getAbsolutePath();
                //获取访问路径
                String dbpath = "http://maybbcal.free.idcfengye.com/dy_app/"+file.getOriginalFilename();
                File fileUpload = new File(filePath);
                if (!fileUpload.exists()) {
                    fileUpload.mkdirs();
                }


                //复制视频到本地服务器
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(filePath,
                                file.getOriginalFilename())));

                out.write(file.getBytes());



                out.flush();
                out.close();

                //视频相关信息保存到数据库中

                Video addVideo=new Video();

                addVideo.setContent(dbpath);
                addVideo.setLikes(0);
                addVideo.setCommentCount(0);
                addVideo.setCreateTime(new Date());
                addVideo.setTitle(title);
                addVideo.setDescription(des);
                addVideo.setVideoUserAvatar("https://tse4-mm.cn.bing.net/th/id/OIP-C.UJn6UIedh0fIZk5V4f_5zgHaE8?pid=ImgDet&rs=1");
                addVideo.setId(id);
                videoDao.publishVideo(addVideo);




            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return new  AjaxResult(AjaxResult.Type.ERROR,"fail");
            } catch (IOException e) {
                e.printStackTrace();
                return new  AjaxResult(AjaxResult.Type.ERROR,"fail");
            }

            return new  AjaxResult(AjaxResult.Type.SUCCESS,"success");

        } else {
            return new  AjaxResult(AjaxResult.Type.ERROR,"fail");
        }

    }
}
